//国际化
$j.extend(loxia.regional['zh-CN'], {
	"ITEM_CATEGORY_TIPS" : "友情提示",
	"ITEM_CATEGORY_CHOOSE_LEAF" : "请选择叶子节点来查看商品！",
	"LABEL_ITEM_CODE" : "商品编码",
	"LABEL_ITEM_TITLE" : "商品名称",
	"LABEL_ITEM_CATEGORY" : "所属分类",
	"ITEM_CATEGORY_CHECK_ERROR" : "错误信息",
	"ITEM_CATEGORY_OPERATE_INFO" : "提示信息",
	"ITEM_CATEGORY_OPERATE_TIP_NOSEL" : "未选中商品或分类",

	"NO_CATEGORY" : "无",
	"RELATE_CATEGORY_NO_CATEGORY_DATA" : "暂无关联行业维护数据",
	"ITEM_CATEGORY_LIST_SUFFIX_TITLE" : "分类商品列表",
	"ITEM_CATEGORY_LIST_DEFAULT_TITLE" : "已分类商品",
	"CATEGORY_FIND" : " 共找到",
	"CATEGORY_RESULT" : "个结果",
	"SORT_UP" : "上移",
	"SORT_DOWN" : "下移",
	"LBAEL_ITEM_OPERATE" : "操作",
	"LBAEL_ITEM_OPERATE_SUCCESS" : "操作成功",
	"LBAEL_ITEM_OPERATE_FAILED" : "操作失败",
	"LABEL_ITEM_SORTNO" : "排序编号",
	"LABEL_ITEM_PICURL" : "商品图片",
	"ITEM_CONFIRM" : "确认操作",
	"ITEM_CONFIRM_ADD_SORT" : "确定添加排序吗？",
	"ITEM_CONFIRM_REMOVE_SORT" : "确定移除排序吗？",
});

// 已分类商品列表url
var itemFindUrl = base + '/product/scope/findItem.json';

var categorySetting = {
	check : {
		enable : true,
		chkStyle: "radio",
		radioType : "all"
	},
	view : {
		dblClickExpand : false,
		showIcon : false
	},
	data : {
		simpleData : {
			enable : true
		}
	},
	callback : {
		onClick : onClick1,
		onCheck : onCheck1,
	},
};

// 操作函数
function buildChooseButtion(data) {

	var itemId = loxia.getObject("id", data);
	var itemName = loxia.getObject("name", data);

	var result = "<a href='javascript:void(0);' iname='" + itemName + "' val='" + itemId
		+ "' class='func-button choose' >选择</a>"
	return result;
}

function onClick1(e, treeId, treeNode) {
	var zTree = $j.fn.zTree.getZTreeObj(treeId);
	zTree.checkNode(treeNode, !treeNode.checked, null, true);
	return false;
}

function onCheck1(e, treeId, treeNode) {
	var zTree = $j.fn.zTree.getZTreeObj(treeId), nodes = zTree.getCheckedNodes(true);
	var sv = "";
	var dv = "";

	for (i in nodes) {
		sv += nodes[i].name + ",";
		dv += nodes[i].id + ",";
	}
	if (sv.length > 0) sv = sv.substring(0, sv.length - 1);
	if (dv.length > 0) dv = dv.substring(0, dv.length - 1);
	var iObj = zTree.setting.inputele;
	$j(iObj).val(sv);
	$j(iObj).attr("dv", dv);
	$j(iObj).parent().change();
}

function resolveCategoryData(t) {
	var v = {}
	v.display = t.val();
	v.value = t.attr("dv");
	return v;
}

function buildCategoryInput(data, options) {
	var elem = document.createElement("input");
	elem.type = "input";
	elem.id = options.id + "_input";
	if (data.display) elem.value = data.display;
	$j(elem).attr("readonly", true);
	$j(elem).addClass(options.inputClass);
	return elem;
}

$j(document).ready(function() {
	var p = {
		columns : [ {
			name : "pid",
			label : "商品",
			inputtype : "custom",
			inputClass : "jr-input-item",
			inputBuilder : buildCategoryInput,
			dataResolver : resolveCategoryData,
		}, {
			name : "cid",
			label : "分类",
			inputtype : "custom",
			inputClass : "jr-input-cate",
			inputBuilder : buildCategoryInput,
			dataResolver : resolveCategoryData,
			columnOperators : [ 'eq', 'ne' ],
		} ],
	};
	var a = $j("._test").jRuleInit(p);
	$j(".ctl-getrule").click(function() {
		var sv = a[0].toUserFriendlyString();
		$j(".sv").html(sv);
		var dv = a[0].toSysString();
		$j(".dv").html(dv);
	});

	$j("body").bind("mousedown", function(event) {
		if ($j(event.target).parents(".ztree").length <= 0) {
			$j(".ztree").fadeOut("fast");
		}
	});

	$j(".jr-input-cate").live('click', function() {
		var iObj = $j(this);
		var iOffset = $j(this).offset();
		var c = $j("#treeDemo");
		c.css({
			left : iOffset.left + "px",
			top : iOffset.top + iObj.outerHeight() + "px"
		}).slideDown("fast");
		$j.fn.zTree.init(c, $j.extend({}, categorySetting, {
			inputele : $j(this)
		}), categoryzNodes);
	});

	$j(".jr-input-item").live('click', function() {
		var inputele = $j(this);

		$j("#dialog-item-select").dialogff({
			type : 'open',
			close : 'in',
			width : '950px',
			height : '600px'
		});
		
		$j("#dialog-item-select .choose").live('click', function() {
			var inputValue = $j(this).attr("val");
			var inputName = $j(this).attr("iname");

			inputele.val(inputName);
			inputele.attr("dv", inputValue);
			inputele.parent().change();
			$j(this).parents("#dialog-item-select").find(".dialog-close").click();
		});
	});

	searchFilter.init({
		formId : 'searchForm',
		searchButtonClass : '.func-button.search'
	});

	$j("#itemTable").loxiasimpletable({
		page : true,
		size : 10,
		nodatamessage : '<span>' + nps.i18n("NO_DATA") + '</span>',
		form : "searchForm",
		cols : [ {
			name : "code",
			label : nps.i18n("LABEL_ITEM_CODE"),
			width : "10%",
		}, {
			name : "name",
			label : nps.i18n("LABEL_ITEM_TITLE"),
			width : "10%",
		}, {
			name : "pirUrl",
			label : nps.i18n("LABEL_ITEM_PICURL"),
			width : "10%",
		}, {
			name : "categoryName",
			label : nps.i18n("LABEL_ITEM_CATEGORY"),
			width : "22%",
		}, {
			name : "sortNo",
			label : nps.i18n("LABEL_ITEM_SORTNO"),
			width : "11%",
		}, {
			label : nps.i18n("LBAEL_ITEM_OPERATE"),
			width : "10%",
			template : "buildChooseButtion"
		} ],
		dataurl : itemFindUrl
	});
	// 筛选列表
	$j(".func-button.search").click(function() {
		$j("#itemTable").loxiasimpletable("refresh");
	});

	$j("#itemTable").loxiasimpletable("refresh");
});